package edu.sias.controller;

import edu.sias.pojo.Post;
import edu.sias.pojo.S_User;
import edu.sias.service.SearchService;
import edu.sias.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/user/showPost")
public class UserPostServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取请求参数
        String name = request.getParameter("name");
        UserService userService = new UserService();
        S_User user = userService.findUserInfoByName(name);
        //用户的id，主要是为了后面根据用户id查找用户发表过的文章
        int id = user.getId();
        //调用业务层，获取数据
        SearchService searchService = new SearchService();
        List<Post> list =
                searchService.findPostsByUserId(id);

        //将数据封装，通过转发返回页面
        request.setAttribute("list",list);
        request.getRequestDispatcher("/showUserPosts.jsp")
                .forward(request,response);

    }
}
